//
//  LikeViewController.m
//  HeartOfMeet
//
//  Created by qianfeng on 16/4/9.
//  Copyright © 2016年 千云锋. All rights reserved.
//

#import "LikeViewController.h"
#import "SpeakDetailViewController.h"
@interface LikeViewController ()

@end

@implementation LikeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.title = @"我的关注";
    
//    self.dataArray = [[NSMutableArray alloc] initWithArray:[LikeSpeakModel MR_findAll]];
}

- (void)deleteClick{
    NSArray *deleteArray = self.tableView.indexPathsForSelectedRows;
    
    NSArray *sortRows = [deleteArray sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2){
        NSIndexPath *indexPath1 = obj1;
        NSIndexPath *indexPath2 = obj2;
        if (indexPath1.row > indexPath2.row) {
            return NO;
        }else{
            return YES;
        }
    }];
    
    for (NSIndexPath *indexPath in sortRows) {
        LikeSpeakModel *model = self.dataArray[indexPath.row];
        NSArray *arr = [LikeSpeakModel MR_findByAttribute:@"id" withValue:model.id];
        for (LikeSpeakModel *model in arr) {
            [model MR_deleteEntity];
        }
        [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
        [self.dataArray removeObjectAtIndex:indexPath.row];
    }
    [self.tableView deleteRowsAtIndexPaths:sortRows withRowAnimation:UITableViewRowAnimationRight];
    
    self.tableView.allowsMultipleSelectionDuringEditing = NO;
    self.tableView.allowsSelectionDuringEditing = NO;
    [self.tableView setEditing:NO animated:YES];
    
    CGFloat y = self.editView.frame.origin.y + 39;
    [UIView animateWithDuration:0.2 animations:^{
        self.editView.frame = CGRectMake(0, y, self.view.bounds.size.width, 35);
    }];
    self.rightBarButtonItem2.enabled = YES;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LeaderCell"];
    
    cell.backgroundView.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    UIView *view = [[UIView alloc] initWithFrame:cell.frame];
    view.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1];
    cell.selectedBackgroundView = view;
    
    LikeSpeakModel *model = self.dataArray[indexPath.row];
    cell.TitleLabe.text = model.speak;
    cell.NameLabel.text = [NSString stringWithFormat:@"%@期",model.topicNum];
    cell.UserLabel.text = @"节目:";
    cell.CoverImageView.layer.cornerRadius = 25;
    cell.PlayImageView.image = [UIImage imageNamed: @"iconfont_"];
    [cell.CoverImageView sd_setImageWithURL:[NSURL URLWithString:model.avaImageUrl] placeholderImage:[UIImage imageNamed: @"placehold_rec"]];
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.tableView.editing == YES) {
        return;
    }
    
    LikeSpeakModel *model = self.dataArray[indexPath.row];
    
    SpeakDetailViewController *speakDetailVC = [[SpeakDetailViewController alloc] init];
    DiantaiModel *diantaiModel = [[DiantaiModel alloc] init];
    
    diantaiModel.id = model.id;
    diantaiModel.user.nickname = model.speak;
    diantaiModel.viewnum = model.viewNum;
    diantaiModel.favnum = model.favNum;
    diantaiModel.user.avatar = model.avaImageUrl;
    

    speakDetailVC.userId = model.id;
    speakDetailVC.nickname = model.speak;
    speakDetailVC.avatar = model.avaImageUrl;
    speakDetailVC.favnum = model.favNum;
    speakDetailVC.viewnum = model.viewNum;
    speakDetailVC.title = model.speak;
    
    [self.navigationController pushViewController:speakDetailVC animated:YES];
}


@end
